import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { wrapperResponse } from 'src/utils';
import { Repository } from 'typeorm';
import { Menu } from './menu.entity';

@Injectable() // 加上注解才能在Controller中去使用
export class MenuService {
  constructor(
    @InjectRepository(Menu)
    private readonly menuRepository: Repository<Menu>,
  ) {}

  findAll() {
    // return new Promise((resolve) => resolve(MENNU_LIST));
    // return this.menuRepository.findBy({ active: 1 });
    return this.menuRepository.query('select * from menu order by id asc');
  }

  findActive() {
    return this.menuRepository.query(
      'select * from menu where active = 1 order by id asc',
    );
  }

  create(body) {
    return wrapperResponse(this.menuRepository.save(body), '菜单创建成功');
  }

  update(body) {
    return wrapperResponse(
      this.menuRepository.update(body.id, body),
      '菜单更新成功',
    );
  }
}
